
package com.fire.core.util;

import java.io.IOException;

import com.fire.core.jkmodel.ReqDTO;
import com.fire.core.jkmodel.XmlMessageDTO;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.jdom.JDOMException;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 类描述：
 * 
 * @author Aspire
 * @version 1.0
 */
public class JkSmsCodeUtils {
    private static Logger logger = Logger.getLogger(JkSmsCodeUtils.class);

    private static String PARTNER_CODE = "YiBaoTong";
    private static String URL = "https://pfe.dev.cmrh.com:8080/pfe/connect";
    private final static String MD5_KEY = "0506e862a9f6f3207a901e0b673ce9de";

    /**
     * 检查签名是否合法
     * 
     * @param xmlMessageDTO
     * @return
     */
    public static boolean checkSign(XmlMessageDTO xmlMessageDTO) {
        String sign = "";
        if (sign.equals(xmlMessageDTO.getSign())) {
            return true;
        }
        return false;
    }

    /**
     * 提交请求
     * 
     * @param reqDTO
     *            请求信息
     * @return 响应内容
     * @throws JDOMException 
     * @throws IOException 
     */
    public static String postRequest(ReqDTO<?> reqDTO, String who) throws Exception {
        String content = JSON.toJSONString(reqDTO);
        String sign = DigestUtils.md5Hex(MD5_KEY + content);
        String postXmlStr = XmlMessageUtil.createXml(PARTNER_CODE, sign, content);
        logger.debug("请求的消息1" + postXmlStr);
        JSONObject json=XmlUtil.xml2JSON(postXmlStr);
//        BlogUtil.outlog(json.toJSONString(), who, "mit/core", "smscode", "req");
        String responseStr = null;
        try {
            responseStr = HttpUtils.request(postXmlStr, URL, 180 * 1000);
            JSONObject jsonResp=XmlUtil.xml2JSON(postXmlStr);
//            BlogUtil.outlog(jsonResp.toJSONString(), who, "mit/core", "smscode", "resq");
            logger.debug("返回消息" + responseStr);
            logger.debug(responseStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseStr;
    }

}
